#!/bin/bash -e

function info() {
echo Usage: `basename $0` '[-lnsgGac] reads1.fq.gz reads2.fq.gz'
exit 65
}

while getopts  ":l:p:f:n:s:gGa:c" opts; do
	case  $opts  in
	a) cut=$OPTARG;;
	c) cnv=T;;
	l) interval=$OPTARG;;
	n) nums=$OPTARG;;
	s) sample_name=$OPTARG;;
	g) single_cell=T;;
	G) somatic=T;;
	p) out_prefix=$OPTARG;;
	f) suffix=$OPTARG;;
	*) info;;
	esac
done
shift $(($OPTIND - 1))


if [ $# -lt 2 ]; then info; fi

r1=`rel_abs.sh $1`
r2=`rel_abs.sh $2` # must run before mkdir=true

makedir=true

if test -z "$interval"; then interval=$data_path/intervals/1/b37.bed; fi


. $var


split_fastq=split_fastq.sh

if test -z "$cut"; then
cutadp=cutadp.sh
elif test "$cut" = "general"; then
cutadp=cutadp.sh
elif test "$cut" = "malbac"; then
cutadp=cutadp_malbac.sh
elif test "$cut" = "ctc80"; then
cutadp=cutadp_ctc80_discard_trimmed.sh
fi

aln=aln_bwa_bt.sh
sam2b=sam2b_samtools.sh
merge=merge_bam.sh
hmmcopy=hmmcopyb38.sh
split_bam=split_bam_0701.sh
extract_bam_bed=extract_bam_bed.sh
markdup=deldup.sh
realn=realign_bt.sh
recal=recal0630.sh
ug=ug.sh
annos=annos0711.sh
combine_vcfs=combine_vcfs.sh
impact=impact.sh
format_simple=format_simple_0629.sh


cpu2=2
cpu4=4
cpu8=8
mem4=4
mem8=8
mem16=16
mem32=32


fq_list=$out_prefix.fq.list.txt
cutadp_list=$out_prefix.cutadp.list.txt
sam_list=$out_prefix.sam.list.txt
bam_list=$out_prefix.bam.list.txt
bed_list=$out_prefix.bed.list.txt
merge_list=$out_prefix.merge.list.txt
split_list=$out_prefix.split.list.txt
markdup_list=$out_prefix.markdup.list.txt

i_bed_list=$out_prefix.i.bed.list.txt
l_bed_list=$out_prefix.l.bed.list.txt
realn_list=$out_prefix.realn.list.txt
recal_list=$out_prefix.recal.list.txt
ug_list=$out_prefix.ug.list.txt
high_list=$out_prefix.high.list.txt
low_list=$out_prefix.low.list.txt
hl_list=$out_prefix.hl.list.txt
combine_list=$out_prefix.combine.list.txt
impact_high_list=$out_prefix.impact.high.list.txt
impact_low_list=$out_prefix.impact.low.list.txt
impact_hl_list=$out_prefix.impact.hl.list.txt
end_list=$out_prefix.end.list.txt


split1_id=$(sub -d"synccount:1" -F -nsplit -m2 -p2 "pigz -cdf $r1|split_fastq.sh -l4000000 -p$out_prefix.1.split -")

split2_id=$(sub -d"syncwith:$split1_id" -F -nsplit -m2 -p2 "pigz -cdf $r2|split_fastq.sh -l4000000 -p$out_prefix.2.split -")

fq_sh=`mktemp fq_list.XXX`
echo "ls $out_prefix.1.split.* > $out_prefix.1.list.txt; ls $out_prefix.2.split.* > $out_prefix.2.list.txt; paste -d' ' $out_prefix.1.list.txt $out_prefix.2.list.txt >$fq_list && rm $out_prefix.1.list.txt $out_prefix.2.list.txt; wc -l $fq_list|cut -d' ' -f1 > nums.txt" > $fq_sh

fq_sh_id=$(sub -d"afterok:$split1_id:$split2_id" -F -nfq -m1 -p1 . $fq_sh) # . may be better than sh




aln2b_sh=`mktemp aln2b_sh.XXX`

echo 'wcl=$(wc -l $fq_list|cut -d" " -f1)

for i in $(seq $wcl); do

cutadp_id[$i]=$(sub -d"afterok:$fq_sh_id" -F -ncutadp.$i -m2 -p1 $cutadp -p$out_prefix.$i $(sed -n "$i"p $fq_list))

aln_id[$i]=$(sub -d"afterok:${cutadp_id[$i]}" -F -naln.$i -m6 -p4 $aln -s$sample_name -t4 $out_prefix.$i.1.cut.fastq $out_prefix.$i.2.cut.fastq)

sam2b_id[$i]=$(sub -d"afterok:${aln_id[$i]}" -F -nsam2b.$i -m4 -p2 $sam2b -t2 $out_prefix.$i.sam)
done' > $aln2b_sh

. $aln2b_sh



. $cmd_done